//
//  CSWTabBarController.m
//  网易彩票
//
//  Created by MAC on 16/6/5.
//  Copyright © 2016年 MAC. All rights reserved.
//

#import "CSWTabBarController.h"

#import "CSWHallViewController.h"
#import "CSWMyLotteryController.h"
#import "CSWFoundViewController.h"
#import "CSWCompeteViewController.h"
#import "CSWHistoryViewController.h"

#import "CSWNavigationController.h"

#import "CSWCompeteNavController.h"

#import "CSWTabBarItem.h"

@interface CSWTabBarController ()<CSWTabBarItemsDelegate>

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation CSWTabBarController

#pragma mark 懒加载
- (NSMutableArray *)items{
    if(_items == nil){
        NSMutableArray *arr = [NSMutableArray array];
        _items = arr;
    }
    return _items;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self setUpAllChildController];
    //自定义TabBar；
    [self setUpTabBar];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 移除系统UITabBar上自带的按钮
    for (UIView *childView in self.tabBar.subviews) {
        // 判断下是否是UITabButton
        if ([childView isKindOfClass:[CSWTabBarItem class]] == NO) {
            
            [childView removeFromSuperview];
        }
    }
}

- (void)setUpTabBar{
    // 1.移除系统的tabBar
//    [self.tabBar removeFromSuperview];
    
    // 2.1 创建UIView,代替系统的tabBar
    CSWTabBarItem *tabBar = [[CSWTabBarItem alloc] initWithFrame:self.tabBar.bounds];

    tabBar.items = self.items;
    tabBar.delegate = self;
    
    [self.tabBar addSubview:tabBar];
    
}

- (void)setUpAllChildController{
    
    // 1.购彩大厅
    CSWHallViewController *hall = [[CSWHallViewController alloc] init];
    // 添加tabBarVc子控制器,并且设置对应对应按钮的内容
    [self setUpOneChildViewController:hall image:[UIImage imageNamed:@"TabBar_LotteryHall_new"] selImage:[UIImage imageNamed:@"TabBar_LotteryHall_selected_new"] title:@"购彩大厅"];
    
    // 2.竞技场
    CSWCompeteViewController *compete = [[CSWCompeteViewController alloc] init];
    [self setUpOneChildViewController:compete image:[UIImage imageNamed:@"TabBar_Arena_new"] selImage:[UIImage imageNamed:@"TabBar_Arena_selected_new"] title:@"竞技场"];
    
    // 3.发现
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"CSWFoundViewController" bundle:nil];
    
    CSWFoundViewController *found = [sb instantiateInitialViewController];
    [self setUpOneChildViewController:found image:[UIImage imageNamed:@"TabBar_Discovery_new"] selImage:[UIImage imageNamed:@"TabBar_Discovery_selected_new"] title:@"发现"];
    
    
    
    // 4.开奖信息
    CSWHistoryViewController *history = [[CSWHistoryViewController alloc] init];
    [self setUpOneChildViewController:history image:[UIImage imageNamed:@"TabBar_History_new"] selImage:[UIImage imageNamed:@"TabBar_History_selected_new"] title:@"开奖信息"];
    
    
    // 5.我的彩票
    CSWMyLotteryController *myLottery = [[CSWMyLotteryController alloc] init];
    [self setUpOneChildViewController:myLottery image:[UIImage imageNamed:@"TabBar_MyLottery_new"] selImage:[UIImage imageNamed:@"TabBar_MyLottery_selected_new"] title:@"我的彩票"];
    
}
    


- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selImage:(UIImage *)selImage title:(NSString *)title
{
    vc.navigationItem.title = title;
    
    UINavigationController *nav = [[CSWNavigationController alloc] initWithRootViewController:vc];
    
    if([vc isKindOfClass:[CSWCompeteViewController class]]){
        nav = [[CSWCompeteNavController alloc] initWithRootViewController:vc];
    }
    
    // 设置对应tabBarButton内容,由对应的子控制器
    nav.tabBarItem.image = image;
    nav.tabBarItem.selectedImage = selImage;
    
    // 保证对应按钮的模型
    [self.items addObject:nav.tabBarItem];
    
    [self addChildViewController:nav];
    
}

-(void)tab:(CSWTabBarItem *)tab btnclick:(NSInteger)btnIndex{
    self.selectedIndex = btnIndex;
}

@end
